package edu.uoc.tds.pac4.beansGestors;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Date;
import java.util.Vector;

import edu.uoc.tds.pac4.DAO.GestorDisc;
import edu.uoc.tds.pac4.DAO.Select;
import edu.uoc.tds.pac4.beans.Centre;
import edu.uoc.tds.pac4.beans.EstAssistentEvent;
import edu.uoc.tds.pac4.common.GestioEventsException;

/**
 * Gestor per al informe estadistic de Assistents a un event
 */

public class GestorEstAssistentEvent extends GestorDisc {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	public GestorEstAssistentEvent() {
		super();
	}
	
	protected final String getColumnes="us.idcard AS us_idcard, us.nom AS us_nom, us.cognoms AS us_cognoms, us.perfilusuari AS us_pefilusuari,"+
									"u.nom AS u_nom";
	
	public Vector<EstAssistentEvent> getInforme(Centre cen, String descripcioEvent, Date dataInici, boolean assistencia) throws GestioEventsException{
		Vector<EstAssistentEvent> vec = new Vector<EstAssistentEvent>();
		// Opcions de filtre
		String sqlWhere = "";
		boolean isFirstWhere = true;

		if(cen != null)
		{
			if(isFirstWhere) isFirstWhere = false;
			else sqlWhere += " AND";
			sqlWhere += " c.codi_centre = " + this.putString(cen.getCodi());
		}
		if((descripcioEvent!=null)&&(descripcioEvent!="")) {
			if(isFirstWhere) isFirstWhere = false;
			else sqlWhere += " AND";
			sqlWhere += " e.descripcio = " + this.putString(descripcioEvent);
		}
		if(dataInici != null) {
			if(isFirstWhere) isFirstWhere = false;
			else sqlWhere += " AND";
			sqlWhere += " e.data_inici = " + this.putDate(dataInici);
		}
		
		if(isFirstWhere) isFirstWhere = false;
		else sqlWhere += " AND";
		sqlWhere += " pen.assitencia = "+ putRealBoolean(assistencia);		
		
		Select sel = new Select("usuaris us ", sqlWhere);
				
		sel.addLeftJoin(" participen pen ", " pen.codi_usuari = us.codi_usuari ");
		sel.addLeftJoin(" event e ", " e.codi_event = pen.codi_event ");
		sel.addLeftJoin(" centres c ", " c.codi_centre = e.centre ");
		sel.addLeftJoin(" universitats u ", " u.codi_universitat = c.universitat ");
		
		sel.addCamp(getColumnes);
		sel.setOrderBy("us.idcard", "ASC");
		
		ResultSet rs = this.llegir(sel);

		if(rs!=null){
			try {
				do {
					EstAssistentEvent eAE = new EstAssistentEvent();
					eAE.buildFromRS(rs);
					vec.add(eAE);
				} while(rs.next());
				rs.close();
			} 
			catch (SQLException e) 
			{
				System.out.print(e.getMessage());
				throw new GestioEventsException(GestioEventsException.Error_BD);
			}
		}
		return vec;
	}


}
